package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IUserDao;
import com.cn.wanxi.dao.impl.UserDaompl;
import com.cn.wanxi.model.OrdbyModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;

import java.util.List;

public class UserServiceImpl implements IUserService {
    @Override
    public int add(UserModel userModel) {
        IUserDao userDao=new UserDaompl();
//        新增用户名1前判断用户名是否存在，存在则返回给页面说明已存在。否则进行新增
        boolean isHave=isHaveFindByUsername(userModel.getUsername());
        if (isHave){
//            返回4代表用户名已存在
            return 4;
        }else {
            int count=userDao.add(userModel);
            return count;
        }
    }

    @Override
    public int login(UserModel userModel) {
        //比较验证码，正确进入下一步，不正确返回-1
        //根据用户名或密码判断该用户是否存在，存在返回1 否再返回f0
        if (!userModel.getCode().equals(userModel.getSessionCode())){
            return -1;
        }else {
            IUserDao iUserDao=new UserDaompl();
            return iUserDao.findByUsernameAndPassword(userModel);
        }
    }



    @Override
    public int deletebyid(int aa) {
        IUserDao iUserDao=new UserDaompl();
        return iUserDao.deletebyid(aa);
    }

    @Override
    public UserModel finById(int aa) {
        IUserDao iUserDao=new UserDaompl();
        return iUserDao.finById(aa);
    }

    @Override
    public int update(UserModel userModel) {
        IUserDao iUserDao=new UserDaompl();
        return iUserDao.update(userModel);
    }

    @Override
    public List<UserModel> findAllBySon(UserModel userModel) {
        IUserDao iUserDao=new UserDaompl();
        List<UserModel> list=iUserDao.findAllBySon(userModel);
        return list;
    }

    @Override
    public int getCount(UserModel userModel) {
        IUserDao iUserDao=new UserDaompl();
        return iUserDao.getCount(userModel);
    }


    //    根据用户名去查数据库是否存在，如果存在返回true 否则返回false
    private  boolean isHaveFindByUsername(String username){
        IUserDao iUserDao = new UserDaompl();
        UserModel userModel = iUserDao.FindByUsername(username);
        boolean isHave = (userModel == null);
        return !isHave;
    }
}

















